package com.byl.modules.sys.service.impl;

import com.byl.common.modules.service.BaseServiceImpl;
import com.byl.modules.sys.dao.MenuDao;
import com.byl.modules.sys.dao.RoleDao;
import com.byl.modules.sys.service.MenuService;
import com.byl.sdk.bean.ResultBean;
import com.byl.sdk.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/24.
 */
@SuppressWarnings("ALL")
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuDao> implements MenuService {

    @Autowired
    MenuDao menuDao;

    @Autowired
    RoleDao roleDao;

    @Override
    public String deleteMenu(Serializable id) {
        //查询该菜单是否有角色引用
        String role = roleDao.queryRoleByMenuId(id);
        if (!StringUtil.isBlank(role))
            return role;
        int rs = menuDao.delete(id);
        if (rs > 0)
            return "success";
        return "fail";
    }

    @Override
    public List queryMenuTreeTable() {
        List list = new ArrayList();
        String rs = menuDao.queryMenuTreeTable();
        if (StringUtil.isBlank(rs))
            return list;
        String[] one = rs.split("~@@~");
        for (int i = 0; i < one.length; i++) {
            String one_tmp = one[i];
            String[] one_menus = one_tmp.split("~!!~");
            //一级菜单
            if (StringUtil.isBlank(one_menus[0]))
                continue;
            Map oneMap = getMap(one_menus[0]);
            if (one_menus.length < 2) {
                list.add(oneMap);
                continue;
            }
            //一级菜单下的二级菜单
            List twolist = new ArrayList();
            String[] two = one_menus[1].split("~&&~");
            for (int j = 0; j < two.length; j++) {
                String two_tmp = two[j];
                String[] two_menus = two_tmp.split("~##~");
                //二级菜单
                if (StringUtil.isBlank(two_menus[0]))
                    continue;
                Map twoMap = getMap(two_menus[0]);
                if (two_menus.length < 2) {
                    twolist.add(twoMap);
                    continue;
                }
                //三级目录
                List threelist = new ArrayList();
                String[] three = two_menus[1].split("@");
                for (int k = 0; k < three.length; k++) {
                    if (StringUtil.isBlank(three[k]))
                        continue;
                    threelist.add(getMap(three[k]));
                }
                twoMap.put("children", threelist);
                twolist.add(twoMap);
            }
            oneMap.put("children", twolist);
            list.add(oneMap);
        }
        return list;
    }

    private Map getMap(String row) {
        Map map = new HashMap();
        String[] gdt = row.split("!");
        map.put("id", gdt[0]);
        map.put("menu_name", gdt[1]);
        map.put("url", getColumn(gdt[2]));
        map.put("perms", getColumn(gdt[3]));
        map.put("menu_type", gdt[4]);
        map.put("icon", getColumn(gdt[5]));
        map.put("sorts", gdt[6]);
        map.put("parent_id", gdt[7]);
        return map;
    }

    private String getColumn(String col) {
        if ("无".equals(col))
            return "";
        return col;
    }

    @Override
    public List queryMenuTree() {
        List list = new ArrayList();
        String rs = menuDao.queryMenuTree();
        if (StringUtil.isBlank(rs))
            return list;
        String[] gdt = rs.split("@@");
        for (int i = 0; i < gdt.length; i++) {
            String menu = gdt[i];
            String[] ddt = menu.split("@");
            if (ddt.length == 0)
                continue;
            String[] tmp = ddt[0].split(",");
            if (tmp.length < 2)
                continue;
            Map<String, Object> map = new HashMap<>();
            map.put("id", tmp[0]);
            map.put("menu_name", tmp[1]);
            if (ddt.length == 1) {
                list.add(map);
                continue;
            }
            String[] mdt = ddt[1].split("!");
            List tmplist = new ArrayList();
            for (int j = 0; j < mdt.length; j++) {
                String[] tm = mdt[j].split(",");
                if (tm.length < 2)
                    continue;
                Map menuMap = new HashMap();
                menuMap.put("id", tm[0]);
                menuMap.put("menu_name", tm[1]);
                tmplist.add(menuMap);
            }
            map.put("child", tmplist);
            list.add(map);
        }
        return list;
    }


    @Override
    public List queryAuthMenuTree() {
        List list = new ArrayList();
        String rs = menuDao.queryMenuTreeTable();
        if (StringUtil.isBlank(rs))
            return list;
        String[] one = rs.split("~@@~");
        for (int i = 0; i < one.length; i++) {
            String one_tmp = one[i];
            String[] one_menus = one_tmp.split("~!!~");
            //一级菜单
            if (StringUtil.isBlank(one_menus[0]))
                continue;
            Map oneMap = getMiniMap(one_menus[0]);
            if (one_menus.length < 2) {
                list.add(oneMap);
                continue;
            }
            //一级菜单下的二级菜单
            List twolist = new ArrayList();
            String[] two = one_menus[1].split("~&&~");
            for (int j = 0; j < two.length; j++) {
                String two_tmp = two[j];
                String[] two_menus = two_tmp.split("~##~");
                //二级菜单
                if (StringUtil.isBlank(two_menus[0]))
                    continue;
                Map twoMap = getMiniMap(two_menus[0]);
                if (two_menus.length < 2) {
                    twolist.add(twoMap);
                    continue;
                }
                //三级目录
                List threelist = new ArrayList();
                String[] three = two_menus[1].split("@");
                for (int k = 0; k < three.length; k++) {
                    if (StringUtil.isBlank(three[k]))
                        continue;
                    threelist.add(getMiniMap(three[k]));
                }
                twoMap.put("children", threelist);
                twolist.add(twoMap);
            }
            oneMap.put("children", twolist);
            list.add(oneMap);
        }
        return list;
    }

    private Map getMiniMap(String row) {
        Map map = new HashMap();
        String[] gdt = row.split("!");
        map.put("id", gdt[0]);
        map.put("menu_name", gdt[1]);
        return map;
    }

    @Override
    public Map getNav(Serializable userId) {
        Map map=new HashMap();
        //查询菜单
        String rs=menuDao.queryNavMenu(userId);
        if(StringUtil.isBlank(rs))
            return map;
        List list=new ArrayList();
        String []gdt=rs.split("~@@~");
        for(int i=0;i<gdt.length;i++){
            String onemenus=gdt[i];
            String []ddt=onemenus.split("~&&~");
            if(ddt.length<2)
                continue;
             Map one=getMap(ddt[0]);
             String []menus=ddt[1].split("@");
             List twolist=new ArrayList();
             for(int j=0;j<menus.length;j++){
                 twolist.add(getMap(menus[j]));
             }
             one.put("child",twolist);
            list.add(one);
        }
        map.put("menulist",list);
        //查询权限
        String pers=menuDao.queryNavMenuBtn(userId);
        map.put("permissions",pers);

        return map;
    }
}
